<template>
  <div class="dashboard-container">
    <header-tool>
      <template #after>
        <el-button type="primary">
          <i class="el-icon-edit" @click="addPermission('0', '1')">添加权限</i>
        </el-button>
      </template>
    </header-tool>
    <el-card class="permisssionCard">
      <el-table
        :data="perData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        default-expand-all
        :tree-props="{ children: 'children' }"
      >
        <el-table-column prop="name" label="菜单名称" sortable width="180">
        </el-table-column>
        <el-table-column prop="code" label="权限标识" sortable width="180">
        </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="{ row }">
            <el-button
              size="mini"
              type="primary"
              v-if="row.type == 1"
              @click="addPermission(row.id, '2')"
              >添加</el-button
            >
            <el-button
              size="mini"
              type="warning"
              @click="editPermission(row.id)"
              >编辑</el-button
            >
            <el-button size="mini" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加弹窗 -->
    <el-dialog :visible.sync="showDialog" :title="title">
      <el-form
        :model="formData"
        label-width="80px"
        :rules="rules"
        style="width:70%;margin:0 auto"
      >
        <el-form-item prop="name" label="权限名称">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item prop="code" label="权限标识">
          <el-input v-model="formData.code"></el-input>
        </el-form-item>
        <el-form-item prop="description" label="权限描述">
          <el-input v-model="formData.description"></el-input>
        </el-form-item>
        <el-form-item label="开关">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          ></el-switch>
        </el-form-item>
        <el-row type="flex" justify="center">
          <el-button type="primary" @click="save">确定</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPermissionList,
  addPermission,
  getPermissionById,
  setPermissionById,
  removePermissionById
} from "@/api/permission";

import { transToTreeData } from "@/utils";
export default {
  name: "permission",
  data() {
    return {
      perData: [],
      showDialog: false,
      formData: {},
      rules: {
        name: {
          required: true,
          message: "请输入权限名称！！！",
          trigger: "blur"
        },
        code: {
          required: true,
          message: "请输入权限标识！！！",
          trigger: "blur"
        }
      }
    };
  },
  methods: {
    // 获取所有权限列表
    async getPerList() {
      let res = await getPermissionList();
      this.perData = transToTreeData(res.data, "0");
    },
    // 点击添加弹窗

    addPermission(pid, type) {
      // 将传过来的pid和type给formData用来发送数据
      this.formData.pid = pid;
      this.formData.type = type;
      this.showDialog = true;
    },

    // 点击弹窗编辑弹窗
    async editPermission(id) {
      let res = await getPermissionById(id);
      this.formData = res.data;
      this.showDialog = true;
    },
    // 取消按钮
    cancel() {
      this.formData = {};
      this.showDialog = false;
    },
    // 保存
    async save() {
      if (this.formData.id) {
        await setPermissionById(this.formData);
      } else {
        await addPermission(this.formData);
      }
      this.$message.success(this.title + "成功！！");
      this.getPerList();
      this.showDialog = false;
      this.formData = {};
    }
  },
  created() {
    this.getPerList();
  },
  computed: {
    title() {
      return this.formData.id ? "编辑权限" : "新增权限";
    }
  }
};
</script>

<style lang="scss" scoped>
.permisssionCard {
  padding: 20px;

  ::v-deep {
    .has-gutter {
      th {
        background-color: #fafafa;
        border-bottom: 2px solid #e8e8e8;
      }
    }
  }
}
</style>
